﻿<%@ Page Language="C#" AutoEventWireup="false" CodeFile="recRestructurform.aspx.cs" Inherits="SIPO.Web.Forms.Transaction.recRestructurform" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Restructur Form</title>
    
    <link href="<%= ResolveUrl("~/styles/chosen/chosen.min.css") %>" rel="stylesheet" type="text/css" />
     <link type="text/css" rel="stylesheet" href="<%= ResolveUrl("~/styles/ui.jqgrid.css") %>" />
    <script src="<%= ResolveUrl("~/scripts/plupload/moxie.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/plupload/plupload.full.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.wrapper.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.inputmask.bundle.min.js") %>" type="text/javascript"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/i18n/grid.locale-en.js") %>"></script>
    <script src="<%= ResolveUrl("~/scripts/chosen/chosen.jquery.min.js") %>" type="text/javascript"></script>
      <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.jqGrid.min.js") %>"></script>

    <script type="text/javascript" language="javascript">
        $(function () {
            $('#ddlDebtor').chosen({ placeholder_text_single: "-- pilih debitur --" });
            $('#ddlJenisRes').chosen({ placeholder_text_single: "-- pilih jenis restruktur --" });
            $('#ddlInvoice').chosen({ placeholder_text_single: "-- pilih no. invoice --" });
            $("#btnLanjut").button({ icons: { primary: "ui-icon-circle-arrow-e"} });
            $("#btnClose").button({ icons: { primary: "ui-icon-close"} });

            $('#btnClose').click(function () {
                parent.$('#dialog').dialog("close");

                return false;
            });
        });
    </script>

    <%--UPLOADER--%>
    <script type="text/javascript">
        $(document).ready(function () {
            $.fn.extend({
                // Converts with every keystroke the hole text to uppercase
                upperCase: function () {
                    $(this).keyup(function (event) {
                        var box = event.target;
                        var txt = $(this).val();
                        var start = box.selectionStart;
                        var end = box.selectionEnd;

                        $(this).val(txt.toUpperCase());
                        box.setSelectionRange(start, end);
                    });
                    return this;
                }
            });
            $('#txtNoSK').upperCase();

            var uploader = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight',
                browse_button: 'pickfiles',
                container: 'container',
                max_file_size: '5mb',
                url: '<%= ResolveUrl("~/AjaxHandler/Transaction/recRestructurhandler.ashx") %>' + '?method=inserttosession',
                flash_swf_url: '<%= ResolveUrl("~/scripts/plupload/Moxie.swf") %>',
                silverlight_xap_url: '<%= ResolveUrl("~/scripts/plupload/Moxie.xap") %>',
                filters: [
            { title: "Pdf files", extensions: "pdf" },
            { title: "Excel files", extensions: "xls,xlsx" },
			{ title: "Text files", extensions: "txt" },
			{ title: "Image files", extensions: "jpeg,jpg,gif,png" },
            { title: "Word files", extensions: "doc,docx" },
			{ title: "PowerPoint files", extensions: "ppt,pptx" }
		],
                resize: { width: 320, height: 240, quality: 90 }
            });

            uploader.bind('Init', function (up, params) {

                var msg = 'Untuk dapat melakukan proses unggah / upload dokumen, browser anda harus mendukung salah satu teknologi, HTML 5 / Flash / Silverlight';

                if (params.runtime == 'html5') {
                    msg = '';
                } else if (params.runtime == 'flash') {
                    msg = '';
                } else if (params.runtime == 'silverlight') {
                    msg = ''
                }

                if (msg != '') {
                    alert(msg);
                }

            });

            uploader.init();

            uploader.bind('FilesAdded', function (up, files) {

                if (up.files.length > 1) {
                    alert('Hanya diperbolehkan satu file untuk di upload!');
                    return false;
                }

                $.each(files, function (i, file) {
                    $('#filelist').append(
                        '<div id="' + file.id + '">' +
                file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' + '</div>');
                });

                up.refresh(); // Reposition Flash/Silverlight
            });

            uploader.bind('FileUploaded', function (up, file, info) {

                var err = JSON.parse(info.response);
                alert(err.Message);

            });

            uploader.bind('Error', function (up, err) {
                $('#filelist').append("<div>Error: " + err.code +
			", Message: " + err.message + (err.file ? ", File: " + err.file.name : "") + "</div>");

                up.refresh(); // Reposition Flash/Silverlight

            });

            $("#txtJmlRestruktur").inputmask("decimal", { radixPoint: ",", autoGroup: true, groupSeparator: ".", groupSize: 3, digits: 2, repeat: 18 });
            $("#txtJmlTagihan").inputmask("decimal", { radixPoint: ",", autoGroup: true, groupSeparator: ".", groupSize: 3, digits: 2, repeat: 18 });

            var date = new Date();
            var currentMonth = date.getMonth();
            var currentDate = date.getDate();
            var currentYear = date.getFullYear();

            $("#txtTglSK").datepicker({ showWeek: true, showButtonPanel: true,
                changeMonth: true, changeYear: true, dateFormat: 'dd/mm/yy', // yearRange: '1990:2030'
                maxDate: new Date(currentYear, currentMonth, currentDate)
            });

            $("#txtTglSK").inputmask("d/m/y", { "placeholder": "dd/mm/yyyy" });

            var id = $('#hfID').val();

            // 1. prepare the validation rules and messages.
            var rules = {
                ddlDebtor: "required",
                txtNoSK: "required",
                txtTglSK: "required",
                ddlJenisRestruktur: "required",
                txtJmlRestruktur: "required",
                txtJmlTagihan: "required"
            };

            var messages = {
                ddlDebtor: "Debitur harus diisi",
                txtNoSK: "Nomor SK harus diisi",
                txtTglSK: "Tanggal SK harus diisi",
                ddlJenisRestruktur: "Jenis Restrukturisasi harus diisi",
                txtJmlRestruktur: "Jumlah Restrukturisasi harus diisi",
                txtJmlTagihan: "Transaksi harus di pilih"
            };

            // 2. Initiate the validator
            var validator = new jQueryValidatorWrapper("form1", rules, messages);
            function callform() {
                parent.$('#dialog').dialog('close');
                // window.alert = function () { };
                if ($('#hfID').val() != '') {
                    var formUrl = '<%= ResolveUrl("~/forms/transaction/recRestructurNextform.aspx") %>';
                    $('#dialog').load(formUrl + '?status=view&edit=1&id=' + $('#hfID').val())
                                        .dialog({ autoOpen: false, modal: true, width: 825, title: 'Schema Pembayaran' });
                    $("#dialog").dialog('open');
                    return false;
                }
                else {
                    var formUrl = '<%= ResolveUrl("~/forms/transaction/recRestructurNextform.aspx") %>';
                    $('#dialog').load(formUrl + '?status=addnew')
                                        .dialog({ autoOpen: false, modal: true, width: 825, title: 'Schema Pembayaran' });
                    $("#dialog").dialog('open');
                    return false;
                }
            }
            // 3. Set the click event to do the validation
            $("#btnLanjut").click(function (e) {
                e.preventDefault();

                if (!validator.validate())
                    return;
                if ($('#ddlJenisRes').val() != ''&& $("#table1").jqGrid('getGridParam', 'records') >0) {
                    var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Transaction/recRestructurhandler.ashx") %>';

                    //                if (id == '') {
                    //                    if (uploader.files.length > 0) // Insert with uploaded file.
                    if (uploader.files.length > 0) {
                        uploader.settings.multipart_params = {
                            'ddlDebtor': encodeURIComponent($('#ddlDebtor').val()),
                            'txtNoSK': encodeURIComponent($('#txtNoSK').val()),
                            'txtTglSK': encodeURIComponent($('#txtTglSK').val()),
                            'ddlJenisRes': encodeURIComponent($('#ddlJenisRes').val()),
                            'txtJmlRestruktur': encodeURIComponent($('#txtJmlRestruktur').val()),
                            'txtJmlTagihan': encodeURIComponent($('#txtJmlTagihan').val()),
                            'iddetail': encodeURIComponent($('#hfParamID').val()),
                            'hfID': encodeURIComponent($('#hfID').val()),
                            'keputusan': encodeURIComponent($("#ddlJenisRes option:selected").text()),
                            'InvoiceNo': encodeURIComponent($("#ddlInvoice").val())
                        };

                        uploader.settings.url = handlerUrl + '?method=inserttosession&iddetail=' + $("#<%=hfParamID.ClientID%>").val();
                        uploader.start();
                        //               setTimeout("callform();", 1500);
                        callform();
                    }
                    else {
                        $.ajax({
                            type: 'POST',
                            url: handlerUrl + "?method=inserttosession" +
                                                        "&ddlDebtor=" + encodeURIComponent($('#ddlDebtor').val()) +
                                                        "&txtNoSK=" + encodeURIComponent($('#txtNoSK').val()) +
                                                        "&txtTglSK=" + encodeURIComponent($('#txtTglSK').val()) +
                                                        "&iddetail=" + encodeURIComponent($('#hfParamID').val()) +
                                                        "&txtJmlTagihan=" + encodeURIComponent($('#txtJmlTagihan').val()) +
                                                        "&ddlJenisRes=" + encodeURIComponent($('#ddlJenisRes').val()) +
                                                        "&txtJmlRestruktur=" + encodeURIComponent($('#txtJmlRestruktur').val()) +
                                                        "&keputusan=" + encodeURIComponent($("#ddlJenisRes option:selected").text()) +
                                                        '&InvoiceNo=' + encodeURIComponent($("#ddlInvoice").val())+
                                                        "&hfID=" + encodeURIComponent($('#hfID').val()),
                            contentType: 'application/json',
                            dataType: 'json'
                        });
                        callform();
                    }
                }
                else {
                    alert("Lengkapi data terlebih dahulu !");
                }
                //                    
                //}
                //                    else {
                //                        $.ajax({
                //                            type: 'POST',
                //                            url: handlerUrl + "?method=inserttosession" +
                //                                    "&ddlDebtor=" + encodeURIComponent($('#ddlDebtor').val()) +
                //                                    "&txtNoSK=" + encodeURIComponent($('#txtNoSK').val()) +
                //                                    "&txtTglSK=" + encodeURIComponent($('#txtTglSK').val()) +
                //                                    "&iddetail=" + encodeURIComponent($('#hfParamID').val()) +
                //                                    "&txtJmlTagihan=" + encodeURIComponent($('#txtJmlTagihan').val()) +
                //                                    "&ddlJenisRes=" + encodeURIComponent($('#ddlJenisRes').val()) +
                //                                    "&txtJmlRestruktur=" + encodeURIComponent($('#txtJmlRestruktur').val()) +
                //                                    "&hfID=" + encodeURIComponent($('#hfID').val()),
                //                            contentType: 'application/json',
                //                            dataType: 'json',
                //                            success: function (data) {
                //                                if (data.IsSuccess) {
                //                                   // window.alert = function () { };
                ////                                    parent.$('#dialog').dialog('close');
                ////                                    var formUrl = '<%= ResolveUrl("~/forms/transaction/recRestructurNextform.aspx") %>';
                ////                                    $('#dialog').load(formUrl + '?status=addnew')
                ////                                        .dialog({ autoOpen: false, modal: true, width: 825, title: 'Schema Pembayaran' });
                ////                                    $("#dialog").dialog('open');
                //                                }
                //                                else {
                //                                    //alert('Error Message: ' + data.Message);
                //                                    parent.$('#dialog').dialog('close');
                //                                    var formUrl = '<%= ResolveUrl("~/forms/transaction/recRestructurNextform.aspx") %>';
                //                                    $('#dialog').load(formUrl + '?status=addnew')
                //                                        .dialog({ autoOpen: false, modal: true, width: 825, title: 'Schema Pembayaran' });
                //                                    $("#dialog").dialog('open');
                //                                }
                //                            },
                //                            error: function (response) {
                //                                var r = JSON.parse(response.responseText);
                //                                alert("Error Message: " + r.Message);
                //                            }
                //                        });
                //                    }
                //                }
                //                else {

                //                    if (uploader.files.length > 0) // Update with uploaded file.
                //                    {
                //                        uploader.settings.multipart_params = {
                //                            'ddlDebtor': encodeURIComponent($('#ddlDebtor').val()),
                //                            'txtNoSK': encodeURIComponent($('#txtNoSK').val()),
                //                            'txtTglSK': encodeURIComponent($('#txtTglSK').val()),
                //                            'ddlJenisRes': encodeURIComponent($('#ddlJenisRes').val()),
                //                            'txtJmlRestruktur': encodeURIComponent($('#txtJmlRestruktur').val()),
                //                            'hfID': encodeURIComponent($('#hfID').val()),
                //                            'txtJmlTagihan': encodeURIComponent($('#txtJmlTagihan').val()),
                //                            'iddetail' : encodeURIComponent($('#hfParamID').val())
                //                        };

                //                        uploader.settings.url = handlerUrl + "?method=inserttosession";
                //                        uploader.start();
                //                        parent.$('#dialog').dialog('close');
                //                        // window.alert = function () { };
                //                        var formUrl = '<%= ResolveUrl("~/forms/transaction/recRestructurNextform.aspx") %>';
                //                        $('#dialog').load(formUrl + '?status=view&edit=1&id=' + $('#hfID').val())
                //                                        .dialog({ autoOpen: false, modal: true, width: 825, title: 'Schema Pembayaran' });
                //                        $("#dialog").dialog('open');

                //                    }
                //                    else {
                //                        $.ajax({
                //                            type: 'POST',
                //                            url: handlerUrl + "?method=inserttosession" +
                //                                    "&ddlDebtor=" + encodeURIComponent($('#ddlDebtor').val()) +
                //                                    "&txtNoSK=" + encodeURIComponent($('#txtNoSK').val()) +
                //                                    "&txtTglSK=" + encodeURIComponent($('#txtTglSK').val()) +
                //                                    "&iddetail=" + encodeURIComponent($('#hfParamID').val()) +
                //                                    "&txtJmlTagihan=" + encodeURIComponent($('#txtJmlTagihan').val()) +
                //                                    "&ddlJenisRes=" + encodeURIComponent($('#ddlJenisRes').val()) +
                //                                    "&txtJmlRestruktur=" + encodeURIComponent($('#txtJmlRestruktur').val()) +
                //                                    "&hfID=" + encodeURIComponent($('#hfID').val()),
                //                            contentType: 'application/json',
                //                            dataType: 'json',
                //                            success: function () {
                //                                if (data.IsSuccess) {
                //                                    // window.alert = function () { };
                //                                    parent.$('#dialog').dialog('close');
                ////                                    var formUrl = '<%= ResolveUrl("~/forms/transaction/recRestructurNextform.aspx") %>';
                ////                                    $('#dialog').load(formUrl + '?status=view&edit=1&id=' + $('#hfID').val())
                ////                                        .dialog({ autoOpen: false, modal: true, width: 825, title: 'Schema Pembayaran' });
                ////                                    $("#dialog").dialog('open');
                //                                }
                //                                else {
                //                                    //                                    alert('Error Message: ' + data.Message);
                //                                    var formUrl = '<%= ResolveUrl("~/forms/transaction/recRestructurNextform.aspx") %>';
                //                                    $('#dialog').load(formUrl + '?status=view&edit=1&id=' + $('#hfID').val())
                //                                        .dialog({ autoOpen: false, modal: true, width: 825, title: 'Schema Pembayaran' });
                //                                    $("#dialog").dialog('open');
                //                                }
                //                            },
                //                            error: function (response) {
                //                                var r = JSON.parse(response.responseText);
                //                                alert("Error Message: " + r.Message);
                //                            }
                //                        });

                //                    }

                //                }

                //setTimeout("parent.$('#table').trigger('reloadGrid');", 3000);


            });

        });
    
    </script>

    <%--Handle Grid--%>
    <script type="text/javascript">

        var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Transaction/recRestructurhandler.ashx") %>';
        var nilaiTagihan = 0, totalTagihan = 0;
        var listId = new Array();
        var demandDetId = new Array();
      //  var IsNonSelect = new Array();
        var first = true;
        function removeA(arr) {
            var what, a = arguments, L = a.length, ax;
            while (L > 1 && arr.length) {
                what = a[--L];
                while ((ax = arr.indexOf(what)) !== -1) {
                    arr.splice(ax, 1);
                }
            }
            return arr;
        }

        $('#table1').jqGrid({
            url: 'nothing',
            datatype: 'json',
            colNames: ['No. Billing', 'Jenis Transaksi', 'Nilai'],
            colModel: [
                        { name: 'BillingNo', index: 'DemandDetailID', width: 0 },
            //                        { name: 'IDTransaksi', index: 'IDTransaksi', width: 100 },
            //                        { name: 'InvoiceNo', index: 'NoSanksi', width: 200 },
                        {name: 'recTransactionType.TransTypeName', index: 'JenisPungutan', width: 135 },
                        { name: 'Value', index: 'Nilai', formatter: 'currency', align: 'right', formatoptions: { decimalSeparator: ".", thousandsSeparator: ",", decimalPlaces: 2 }, width: 150 }
           	    ],
            rowNum: 100000,
            id: 'BillingNo',
            multiselect: true,
            rowList: [5, 10, 15, 20],
            //pager: '#pager1',
            sortname: 'BillingNo',
            sortorder: "asc",
            viewrecords: true,
            jsonReader: { repeatitems: false },
            width: 700,
            cellEdit: true,
            cellsubmit: 'clientArray',
            height: 100,
            caption: 'Transaksi',
            ajaxGridOptions: { cache: false },
            //            beforeSelectRow: function (rowid, e) {
            //                if ($('#hfID').val() == '') {
            //                    var ok = false;
            //                    var ret = jQuery('#table1').jqGrid('getRowData', rowid);
            //                    for (i = 0; i < IsNonSelect.length; i++) {
            //                        if (IsNonSelect[i] == ret.DemandDetailID) {
            //                            ok = true;
            //                            //  alert(IsNonSelect[i]);
            //                            //  alert(ret.DemandDetailID);
            //                        }
            //                    }

            //                    if (ok == true) {
            //                        return false;
            //                    }
            //                    else {
            //                        // alert();
            //                        return true;
            //                    }
            //                }
            //                else
            //                    return true;
            //            },
            onSelectRow: function (id, status) {
                var rowData = jQuery(this).getRowData(id);

                //nilaiTagihan = parseFloat(rowData['Nilai']).toFixed(2);
                //totalTagihan = parseFloat($("#<%=txtJmlTagihan.ClientID%>").val()).toFixed(2);
                nilaiTagihan = parseInt(rowData['Value']);
                //totalTagihan = ($("#txtJmlTagihan").val());
                //alert(totalTagihan);
                //                if (status) {
                //  alert(nilaiTagihan);
                totalTagihan = (((totalTagihan) * 1) + ((nilaiTagihan) * 1));

                listId.push(id);
                demandDetId.push(rowData['BillingNo']);
                //                } else {
                //                    //                    totalTagihan = totalTagihan.replace(".", "");
                //                    totalTagihan = (totalTagihan - nilaiTagihan);
                //                    removeA(listId, id);
                //                    removeA(demandDetId, rowData['BillingNo']);
                //                }

                rowChecked = 1;
                currentrow = id;
                //  $("#txtJmlTagihan").val('');
                $("#txtJmlTagihan").val(totalTagihan);
                // if (first ==  false) {
                $("#txtJmlRestruktur").val(totalTagihan);
                //  }
                $("#<%=hfParamID.ClientID%>").val(demandDetId);
                if ($("#txtJmlTagihan").val() == 0) {
                    $("#txtJmlTagihan").val('');
                    $("#txtJmlRestruktur").val('');
                }
                //   alert(demandDetId);
                return false;
            },
            onSelectAll: function (ids, status) {
                var dontAdd = false;

                if (status) {
                    totalTagihan = Number($("#<%=txtJmlTagihan.ClientID%>").val());
                    //totalTagihan = parseFloat($("#<%=txtJmlTagihan.ClientID%>").val()).toFixed(2);

                    for (var i = 0; i < ids.length; i++) {
                        var rowData = jQuery(this).getRowData(ids[i]);

                        for (var j = 0; j < listId.length; j++) {
                            if (listId[j] == ids[i]) dontAdd = true;
                        }

                        if (!dontAdd) {
                            var rowData = jQuery(this).getRowData(ids[i]);
                            nilaiTagihan = Number(rowData['Nilai']);
                            totalTagihan += nilaiTagihan;
                            demandDetId.push(rowData['DemandDetailID']);
                        }

                        dontAdd = false;
                    }

                    listId = [];
                    listId = ids;
                    $("#txtJmlTagihan").val(totalTagihan);
                    $("#txtJmlRestruktur").val(totalTagihan);
                    //  $("#<%=hfParamID.ClientID%>").val(demandDetId);
                    if ($("#txtJmlTagihan").val() == 0) {
                        $("#txtJmlTagihan").val('');
                        $("#txtJmlRestruktur").val('');
                    }


                } else {
                    totalTagihan = 0;
                    listId = [];
                    demandDetId = [];
                }

                $("#<%=txtJmlTagihan.ClientID%>").val(totalTagihan);
                $("#<%=hfParamID.ClientID%>").val(demandDetId);
            },
            loadComplete: function (data) {
                $("#<%=txtJmlTagihan.ClientID%>").val();
                $('#cb_table1').attr('disabled', true);

                //                $.ajax({
                //                    type: 'POST',
                //                    url: '<%= ResolveUrl("~/ajaxhandler/transaction/recRestructurhandler.ashx") %>' + '?method=GetDetailByDebtId&DebtID=' + $('#ddlDebtor').val(),
                //                    contentType: 'application/json',
                //                    dataType: 'json',
                //                    success: function (rows) {

                //                        $.each(rows, function (r, row) {
                for (var i = 0; i < data.rows.length; i++) {
                    var idx = i + 1;
                    //  if (data.rows[i].CreatedBy=='centang') {

                    $('#table1').jqGrid('setSelection', idx);
                    var chk = $("#jqg_table1_" + idx);
                    chk.attr('disabled', true);
                    // }
                }
               // alert(demandDetId);
                //                        });
                $("#<%=hfParamID.ClientID%>").val(demandDetId);
                // alert();
                //                if ($('#hfID').val() == '') {
                //                    $("#txtJmlTagihan").val('');
                //                    $("#txtJmlRestruktur").val('');
                //                    totalTagihan = "";
                //                    demandDetId = new Array();
                //                }
                first = false;
                //alert(demandDetId);
                //alert(demandDetId);
                //                        for (var i = 0; i < data.rows.length; i++) {
                //                            for (var x = 0; x < idsOfSelectedRows.length; x++) {

                //                                if (data.rows[i].SubSubSectorID == idsOfSelectedRows[x]) {
                //                                    var idx = i + 1;
                //                                    $('#tblSubSubSector').jqGrid('setSelection', idx);
                //                                }
                //                            }
                //                        }

                //                    },
                //                    error: function () {
                //                        alert("Failed to load User Sector");
                //                    }
                //                });
                //  }
            }
        });

        $("#table1").jqGrid('navGrid', '#pager1', { edit: false, add: false, search: false, del: false, refreshtext: 'Refresh' },
            {}, {}, {}, { closeOnEscape: true, multipleSearch: false, closeAfterSearch: true });     
    </script>

    <script type="text/javascript">



        $("#ddlDebtor").change(function () {
           // alert($("#ddlDebtor").val());
            getinvoice($(this).val());
            $("#table1").jqGrid('clearGridData');
            $("#txtJmlTagihan").val('');
            $("#txtJmlRestruktur").val('');
        });
        $("#ddlInvoice").change(function () {
            $("#txtJmlTagihan").val('');
            $("#table1").jqGrid('clearGridData');
            $("#txtJmlRestruktur").val('');
            demandDetId = new Array();
            totalTagihan = 0;
            refreshGrid($(this).val());
        });

        function refreshGrid(InvoiceNo) {

            //setTimeout("$('#table1').trigger('reloadGrid');", 2000);
            $("#table1").jqGrid('setGridParam', { url: handlerUrl + '?method=gettransbydebtid&debtid=' + InvoiceNo + '&ResID=' + $('#hfID').val().toString() }).trigger('reloadGrid');
        }
        if ($('#hfID').val().toString() != "") {
           // getinvoice($("#ddlDebtor").val());
            refreshGrid($("#ddlInvoice").val());
           
            setTimeout("$('#table1').trigger('reloadGrid');", 750);
        }

        function getinvoice(debtID) {
            if (debtID!="") {
                //$("#trVersi").show();
                if ($('#hfID').val().toString() != "") {
                    var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getinvoice.ashx?method=getbydebtor&edit=1") %>';
                }
                else {
                    var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getinvoice.ashx?method=getbydebtor") %>';
                }
                $citys = $("#ddlInvoice");
                $citys.get(0).options.length = 0;
                $citys.get(0).options[0] = new Option("-- pilih no. invoice --", "");
                $.ajax({
                    type: 'POST',
                    url: handlerUrl + '&DebtorID=' + debtID + '&ResID=' + $('#hfID').val(),
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (datas) {

                        if (datas.length > 1) {
                            //    $citys.get(0).options[0] = new Option("versi", "");
                            $.each(datas, function (i, data) {
                                $citys.append('<option value="' + data.InvoiceNo + '">' + data.InvoiceNo + '</option>');
                            });

                        } else if (datas.length == 1) {
                            $citys.append('<option value="' + datas[0].InvoiceNo + '">' + datas[0].InvoiceNo + '</option>');
                        }

                        $citys.trigger('chosen:updated');

                    },
                    error: function () {
                        alert('Error!');
                    }
                });
            }
            else {
                $("#trVersi").hide();
                $citys = $("#ddlInvoice");
                $citys.get(0).options.length = 0;
                $citys.get(0).options[0] = new Option("-- No. Invoice --", "");
                $citys.trigger('chosen:updated');
            }
        }

    </script>
     
  
</head>
<body>
    <form id="form1" runat="server">
    <table border="0">
        <tr>
            <td style="width: 50">
                Nama Debitur
            </td>
            <td>
                :
            </td>
            <td>
                <asp:DropDownList ID="ddlDebtor" runat="server" Width="300">
                </asp:DropDownList>
                &nbsp;<span style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                No. Surat
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtNoSK" runat="server" Width="200" MaxLength="20"></asp:TextBox>&nbsp;
                <span style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Tgl. Surat
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtTglSK" runat="server" Width="100" MaxLength="10"></asp:TextBox>&nbsp;
                <span style="color: Red;">*</span>
            </td>
        </tr>
         <tr>
            <td style="width: 50">
                No Invoice
            </td>
            <td>
                :
            </td>
            <td>
                <asp:DropDownList ID="ddlInvoice" runat="server" Width="300">
                </asp:DropDownList>
                &nbsp;<span style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Transaksi
            </td>
            <td>
                :
            </td>
            <td>
                <div>
                    <table id="table1" class="scroll" cellpadding="0" cellspacing="0">
                    </table>
                    <div id="pager1" class="scroll">
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Jumlah Tagihan
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtJmlTagihan" Style="text-align: right" runat="server" Width="200" ReadOnly="true"></asp:TextBox>&nbsp;<span
                    style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Jenis Restrukturisasi
            </td>
            <td>
                :
            </td>
            <td>
                <asp:DropDownList ID="ddlJenisRes" runat="server" Width="300">
                </asp:DropDownList>
                &nbsp;<span style="color: Red;">*</span>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Jumlah Restrukturisasi
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtJmlRestruktur" Style="text-align: right" runat="server" Width="200"></asp:TextBox>&nbsp;<span
                    style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Lampiran
            </td>
            <td>
                :
            </td>
            <td>
                <div id="container">
                    <a id="pickfiles" class="plupload_button plupload_add ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary"
                        href="#"><span class="ui-button-icon-primary ui-icon ui-icon-circle-plus" /><span
                            class="ui-button-text">Add Files</span> </a>
                    <br />
                    <div id="filelist"></div>
                </div>
            </td>
        </tr>
    </table>
    <br />    
    <br />
    <div style="text-align: right; padding-top: 10px;">
        <button id="btnLanjut" type="button">
            Lanjut</button>
        <button id="btnClose" type="button">
            Batal</button>
    </div>
    <asp:HiddenField ID="hfID" runat="server" />
    <asp:HiddenField ID="hfParamID" runat="server" />
    </form>
   
</body>
</html>
